package com.demo.asynctool.worker;

import com.jd.platform.async.callback.ICallback;
import com.jd.platform.async.callback.IWorker;
import com.jd.platform.async.worker.WorkResult;
import com.jd.platform.async.wrapper.WorkerWrapper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class AWorker implements IWorker<Long, String>, ICallback<Long, String> {
    @Override
    public void begin() {
        System.out.println("A接口--线程名称：" + Thread.currentThread().getName() + "- 开始时间 --" + System.currentTimeMillis());
    }

    public void result(boolean b, Long aLong, WorkResult<String> workResult) {
    }

    @SneakyThrows
    @Override
    public String action(Long aLong, Map<String, WorkerWrapper> map) {
        StopWatch stopWatch = new StopWatch("task");
        stopWatch.start("A接口");
        TimeUnit.MILLISECONDS.sleep(200);
        stopWatch.stop();
        System.out.println("A接口耗时：" + stopWatch.getTotalTimeMillis() + "毫秒");
        return "A";
    }

    @Override
    public String defaultValue() {
        return "A接口默认返回值";
    }
}
